package com.agent.crm.db;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.agent.crm.bean.NoteBookDomain;

public class NoteBookDao {
	private DatabaseManager manager = null;
	//private Context context = null;
	
	public NoteBookDao(Context context){

		//this.context = context;
		manager = new DatabaseManager(context);
	}
	
	public void saveOrUpdate(NoteBookDomain note){
		if(0!=note.getNoteBookId()){
			update(note);
		}else{
			save(note);
		}
	}
	
	public void save(NoteBookDomain note){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("noteItemTitle", note.getNoteItemTitle());
		values.put("noteItemTitleContent", note.getNoteItemTitleContent());
		values.put("noteItemLimitDate", note.getNoteItemLimitDate());
		try{
			db.insert("t_note_book", null, values);
		}finally{
			db.close();
		}
	}
	
	public String[] formatSechduleColumn(){
		String[] columnArr=new String[DBInitScript.noteBook_column.size()];
		Iterator iter = DBInitScript.noteBook_column.entrySet().iterator();
		int i=0;
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = entry.getKey().toString();
			columnArr[i]=key;
			i++;
		}
		return columnArr;
	}
	
	public List<NoteBookDomain> getAllNoteBook(){
		//manager = new manager(context, "schedules.db");
		List<NoteBookDomain> notes =new ArrayList<NoteBookDomain>();
		SQLiteDatabase db = manager.getWritableDatabase();
		Cursor cursor = db.query("t_note_book",formatSechduleColumn(), "", new String[]{}, null, null, "noteItemLimitDate desc");
		while(cursor.moveToNext()){
			NoteBookDomain o=new NoteBookDomain(
					cursor.getInt(cursor.getColumnIndex("noteBook_id")),
					cursor.getString(cursor.getColumnIndex("noteItemTitle")),
					cursor.getString(cursor.getColumnIndex("noteItemTitleContent")),
					cursor.getString(cursor.getColumnIndex("noteItemLimitDate")));
					notes.add(o);
		}
		cursor.close();
		return notes;
	}
	
	
	public void update(NoteBookDomain note){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
			String sql= " update t_note_book set noteItemTitle='"+note.getNoteItemTitle()+"'"+
					    " ,noteItemTitleContent ='"+note.getNoteItemTitleContent()+"'"+
					    " ,noteItemLimitDate='"+note.getNoteItemLimitDate()+"'"+
					    " where noteBook_id='"+note.getNoteBookId()+"'";
			db.execSQL(sql);
			db.close();
	}
	
	public void delete(String id){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
			String sql= " delete from t_note_book  where noteBook_id="+id+"";
			db.execSQL(sql);
			db.close();
	}
	
	/**
	 *关闭DB
	 */
	public void destoryDB(){
		if(manager != null){
			manager.close();
		}
	}
}
